/**
 * Created by Administrator on 2016/4/12.
 */

//显示提示信息
function showPromptMsg_Success(text){
    if(typeof(toastr)== "undefined"){
        stopPromptMsg()
        $("#promptMsg_Success").show()
        $("#promptMsg_Success").html(text).fadeOut(3000);
    }else{
        toastr.success(text)
    }
}

function showPromptMsg_Info(text){
    if(typeof(toastr)== "undefined"){
        stopPromptMsg()
        $("#promptMsg_Info").show()
        $("#promptMsg_Info").html(text).fadeOut(3000);
    }else{
        toastr.info(text)
    }
}

function showPromptMsg_Warning(text){
    if(typeof(toastr)== "undefined"){
        stopPromptMsg()
        $("#promptMsg_Warning").show();
        $("#promptMsg_Warning").html(text).fadeOut(3000);
    }else{
        toastr.warning(text)
    }
}

function showPromptMsg_Danger(text){
    if(typeof(toastr)== "undefined"){
        stopPromptMsg()
        $("#promptMsg_Danger").show()
        $("#promptMsg_Danger").html(text).fadeOut(3000);
    }else{
        toastr.error(text)
    }
}

function stopPromptMsg(){
    $("#promptMsg_Success").stop(true,true);
    $("#promptMsg_Info").stop(true,true);
    $("#promptMsg_Warning").stop(true,true);
    $("#promptMsg_Danger").stop(true,true);
}


//显示loading加载标志
function loadingStart(){
    $("#loading").show()
    $("#loadingCover").show()

}

function loadingEnd(){
    $("#loading").hide()
    $("#loadingCover").hide()
}


function checkURL(URL){
    var str=URL;
    str = str.match(/http(s):\/\/.+/);
    if (str == null){
        return false;
    }else{
        return true;
    }
}


function paginatorReq(url, pageName,pageSize,pageLength, $scope, $http, callback) {
    if (sessionStorage.getItem(pageName) == ""||sessionStorage.getItem(pageName) == null) {
        sessionStorage.setItem(pageName, 1);
    }
    $scope.currPage = sessionStorage.getItem(pageName);
    $scope.pageSize = pageSize;
    $scope.pageLength = pageLength;
    function reqData() {
        loadingStart()
        $http({
            url: url,
            data: {pageNum: sessionStorage.getItem(pageName), pageSize: $scope.pageSize},
            method: "POST"
        }).success(function (data) {
            $scope.pageCount = data.data.PageCount
            $scope.pageList = []

            var currP = parseInt(sessionStorage.getItem(pageName));
            var len = parseInt($scope.pageLength);
            var pageSum = parseInt($scope.pageCount);

            if (currP > 1) {
                if (currP - 1 + len <= pageSum) {
                    for (var i = currP - 1; i<currP - 1 + len; i++) {
                        var page = {
                            id: i
                        }
                        $scope.pageList.push(page);
                    }
                }
                else{
                    var initPosition = pageSum-len>0?pageSum-len+1:1
                    for (var i = initPosition ; i<=pageSum; i++) {
                        var page = {
                            id: i
                        }
                        $scope.pageList.push(page);
                    }
                }
            }
            else{
                if (currP  + len <= pageSum) {
                    for (var i = currP ; i<currP  + len; i++) {
                        var page = {
                            id: i
                        }
                        $scope.pageList.push(page);
                    }
                }
                else{
                    for (var i = 1; i<=pageSum ; i++) {
                        var page = {
                            id: i
                        }
                        $scope.pageList.push(page);
                    }
                }
            }
            callback(data.data)
        }).finally(function(){
            //load结束，不管成功与否
            loadingEnd();
        })
    }

    reqData()
    $scope.change = function (num) {
        sessionStorage.setItem(pageName, num)
        $scope.currPage = num;
        reqData()
    }
}